package com.phonetools.commonutils.android.hardware

import android.app.Activity
import android.hardware.Camera
import android.hardware.Camera.CameraInfo
import android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK
import android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT
import android.util.Log
import android.view.Surface.*

fun setCameraDisplayOrientation(activity: Activity, cameraId: Int) {
    val info = CameraInfo()
    Camera.getCameraInfo(cameraId, info)
    val rotation = activity.windowManager.defaultDisplay.rotation
    var degrees = 0
    when (rotation) {
        ROTATION_0 -> degrees = 0
        ROTATION_90 -> degrees = 90
        ROTATION_180 -> degrees = 180
        ROTATION_270 -> degrees = 270
    }
    var result: Int
    if (info.facing == CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360
        result = (360 - result) % 360  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360
    }
    val camera = Camera.open(cameraId)
    camera.setDisplayOrientation(result)
    camera.release()
}

fun setCameraDisplayOrientation(cameraId: Int, rotation: Int) {
    val info = CameraInfo()
    Camera.getCameraInfo(cameraId, info)
    var degrees = 0
    when (rotation) {
        ROTATION_0 -> degrees = 0
        ROTATION_90 -> degrees = 90
        ROTATION_180 -> degrees = 180
        ROTATION_270 -> degrees = 270
    }
    var result: Int
    if (info.facing == CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360
        result = (360 - result) % 360  // compensate the mirror
        Log.d("CameraUtils", "CAMERA_FACING_FRONT:$result")
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360
        Log.d("CameraUtils", "CAMERA_FACING_BACK:$result")
    }
    val camera = Camera.open(cameraId)
    camera.setDisplayOrientation(result)
    camera.release()
}

fun setAllCameraDisplayOrientation(activity: Activity) {
    val numberOfCameras = Camera.getNumberOfCameras()
    if (numberOfCameras <= 0) return
    for (i in 0 until numberOfCameras) {
        setCameraDisplayOrientation(activity, i)
    }
}

fun setAllCameraDisplayOrientation(rotation: Int) {
    val numberOfCameras = Camera.getNumberOfCameras()
    if (numberOfCameras <= 0) return
    for (i in 0 until numberOfCameras) {
        setCameraDisplayOrientation(i, rotation)
    }
}

fun getCameraInfoList(): List<CameraInfo> {
    val numberOfCameras = Camera.getNumberOfCameras()
    if (numberOfCameras <= 0) return emptyList()
    val list: MutableList<CameraInfo> = mutableListOf()
    Log.d("CameraUtils", "numberOfCameras:$numberOfCameras")
    for (i in 0 until numberOfCameras) {
        Log.d("CameraUtils", "i:$i")
        val info = CameraInfo()
        Camera.getCameraInfo(i, info)
        list.add(info)
        when (info.facing) {
            CAMERA_FACING_BACK -> {
                Log.d("CameraUtils", "Camera found back:$i")
            }
            CAMERA_FACING_FRONT -> {
                Log.d("CameraUtils", "Camera found front:$i")
            }
        }
    }
    return list
}